extends CharacterBody2D

# 基础属性
@export var max_health = 30
@export var current_health = 30
@export var movement_speed = 100.0
@export var damage = 10
@export var experience_value = 5
@export var attack_range = 20.0

# 状态标记
var is_elite = false
var is_boss = false
var target_player = null
var is_frozen = false
var is_burning = false
var is_slowed = false

# 状态效果计时器
var burn_timer = 0.0
var burn_damage = 0
var burn_interval = 1.0
var time_since_last_burn = 0.0

var slow_timer = 0.0
var slow_factor = 1.0

var freeze_timer = 0.0

# 掉落物相关
var experience_orb_scene = preload("res://ExperienceOrb.tscn")
var item_drop_chance = 0.1  # 物品掉落几率

# 视觉效果
@onready var sprite = $Sprite2D
@onready var animation_player = $AnimationPlayer
@onready var health_bar = $HealthBar

# 信号
signal enemy_died(enemy)

func _ready():
	# 添加到敌人组
	add_to_group("enemies")
	
	# 初始化生命值条
	if health_bar:
		health_bar.max_value = max_health
		health_bar.value = current_health
		health_bar.visible = false  # 默认隐藏，受伤时显示
	
	# 如果是精英或Boss，调整外观
	if is_elite:
		if sprite:
			sprite.modulate = Color(1.2, 0.8, 0.8)  # 精英敌人偏红色
			sprite.scale = Vector2(1.3, 1.3)  # 精英敌人更大
	elif is_boss:
		if sprite:
			sprite.modulate = Color(1.5, 1.0, 0.5)  # Boss偏金色
			sprite.scale = Vector2(2.0, 2.0)  # Boss更大
	
	# 查找玩家
	target_player = get_tree().get_first_node_in_group("player")

func _physics_process(delta):
	if is_frozen:
		# 冰冻状态下不移动
		return
	
	if target_player and is_instance_valid(target_player):
		# 计算到玩家的方向
		var direction = (target_player.global_position - global_position).normalized()
		
		# 应用减速效果
		var actual_speed = movement_speed * slow_factor
		
		# 设置速度并移动
		velocity = direction * actual_speed
		move_and_slide()
		
		# 检查是否在攻击范围内
		if global_position.distance_to(target_player.global_position) <= attack_range:
			attack_player()
	
	# 处理燃烧效果
	if is_burning:
		time_since_last_burn += delta
		if time_since_last_burn >= burn_interval:
			time_since_last_burn = 0
			take_damage(burn_damage)
		
		burn_timer -= delta
		if burn_timer <= 0:
			is_burning = false
	
	# 处理减速效果
	if is_slowed:
		slow_timer -= delta
		if slow_timer <= 0:
			is_slowed = false
			slow_factor = 1.0
	
	# 处理冰冻效果
	if is_frozen:
		freeze_timer -= delta
		if freeze_timer <= 0:
			is_frozen = false
			if sprite:
				sprite.modulate = Color(1, 1, 1) if not is_elite and not is_boss else sprite.modulate

func attack_player():
	# 只有在有动画播放器且未在攻击动画中时才攻击
	if animation_player and not animation_player.is_playing():
		if animation_player.has_animation("attack"):
			animation_player.play("attack")
		
		# 对玩家造成伤害
		if target_player.has_method("take_damage"):
			target_player.take_damage(damage)

func take_damage(amount):
	current_health -= amount
	
	# 显示生命值条
	if health_bar:
		health_bar.value = current_health
		health_bar.visible = true
		
		# 创建计时器在一段时间后隐藏生命值条
		var timer = get_tree().create_timer(3.0)
		timer.connect("timeout", _on_health_bar_timer_timeout)
	
	# 播放受伤动画
	if animation_player and animation_player.has_animation("hit"):
		animation_player.play("hit")
	
	# 受伤闪烁效果
	if sprite:
		sprite.modulate = Color(1.5, 1.5, 1.5)  # 变亮
		var tween = create_tween()
		if is_elite:
			tween.tween_property(sprite, "modulate", Color(1.2, 0.8, 0.8), 0.2)
		elif is_boss:
			tween.tween_property(sprite, "modulate", Color(1.5, 1.0, 0.5), 0.2)
		else:
			tween.tween_property(sprite, "modulate", Color(1, 1, 1), 0.2)
	
	# 检查是否死亡
	if current_health <= 0:
		die()

func die():
	# 发出信号
	emit_signal("enemy_died", self)
	
	# 生成经验球
	var xp_value = experience_value
	if is_elite:
		xp_value *= 5
	elif is_boss:
		xp_value *= 20
	
	var orb = experience_orb_scene.instantiate()
	get_tree().get_root().add_child(orb)
	orb.global_position = global_position
	orb.xp_amount = xp_value
	
	# 随机掉落物品
	if randf() < item_drop_chance or is_elite or is_boss:
		drop_item()
	
	# 播放死亡动画
	if animation_player and animation_player.has_animation("death"):
		animation_player.play("death")
		# 等待动画完成后删除
		await animation_player.animation_finished
		queue_free()
	else:
		# 如果没有死亡动画，直接删除
		queue_free()

func drop_item():
	# 这里可以实现物品掉落逻辑
	# 例如生成金币、药水或其他物品
	pass

func apply_burn_effect(damage_per_tick, duration):
	is_burning = true
	burn_damage = damage_per_tick
	burn_timer = duration
	time_since_last_burn = 0
	
	# 视觉效果
	if sprite:
		sprite.modulate = Color(1.3, 0.7, 0.7)  # 偏红色
		var tween = create_tween()
		if is_elite:
			tween.tween_property(sprite, "modulate", Color(1.2, 0.8, 0.8), 0.3)
		elif is_boss:
			tween.tween_property(sprite, "modulate", Color(1.5, 1.0, 0.5), 0.3)
		else:
			tween.tween_property(sprite, "modulate", Color(1, 1, 1), 0.3)

func apply_slow_effect(slow_multiplier, duration):
	is_slowed = true
	slow_factor = slow_multiplier
	slow_timer = duration
	
	# 视觉效果
	if sprite:
		sprite.modulate = Color(0.7, 0.7, 1.3)  # 偏蓝色
		var tween = create_tween()
		if is_elite:
			tween.tween_property(sprite, "modulate", Color(1.2, 0.8, 0.8), 0.3)
		elif is_boss:
			tween.tween_property(sprite, "modulate", Color(1.5, 1.0, 0.5), 0.3)
		else:
			tween.tween_property(sprite, "modulate", Color(1, 1, 1), 0.3)

func apply_freeze_effect(duration):
	is_frozen = true
	freeze_timer = duration
	
	# 视觉效果
	if sprite:
		sprite.modulate = Color(0.5, 0.8, 1.5)  # 冰蓝色

func scale_stats(multiplier):
	# 根据乘数调整敌人属性
	max_health = int(max_health * multiplier)
	current_health = max_health
	damage = int(damage * multiplier)
	experience_value = int(experience_value * multiplier)
	
	# 更新生命值条
	if health_bar:
		health_bar.max_value = max_health
		health_bar.value = current_health

func _on_health_bar_timer_timeout():
	if health_bar:
		health_bar.visible = false
